/*** vender ***/
package comm;

import db.DataBase;
import java.io.*;
import java.net.*;
import util.Server;
import util.Utils;

public class Communication {

    private static boolean isConnected;
    private static Socket kkSocket = null;
    public static PrintWriter out = null;
    public static BufferedReader in = null;
    public static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    public static String fromServer;
    public static String fromUser;

    public static boolean isConnected() {
        return isConnected;
    }

    public static void connect() {
        //KnockKnockClient.execute();
        if (isConnected) {
            return;
        }
        try {
            kkSocket = new Socket(Server.getServer(), Server.getPort());
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

        isConnected = true;
    }

    public static String read() {
        try {
            fromServer = in.readLine();
            if (fromServer != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye.")) { //tentar alterar parada
                    System.out.println("Passou aqui.");
                    disconnect();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fromServer;
    }

    public static String writeAndRead(String data) {
        try {
            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye.")) {
                    break;
                }
                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fromServer;
    }

    public static void write(String data) {

        fromUser = data;

        out.println(fromUser);
        if (fromUser != null) {
            System.out.println("Client: " + fromUser);
        }


    }

    public static void disconnect() {
        try {
            out.close();
            in.close();
            stdIn.close();
            kkSocket.close();
            isConnected = false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*******************************************
     * completar com chamadas ao servidor central
     */
    public static String[][] getData(int type) {
        if (type == DataBase.PRODUCTS) {
            write("PRODUTOS");
            int numProd = Integer.valueOf(read());
            String[][] list = new String[numProd][];
            for(int i = 0; i< numProd; i++){
                list[i] = read().split("#");
            }
            return list;

        }else{
            return new String[10][10];
        }
    }

    public static void writeData(int type, String[][] data) {
        if (type == DataBase.SALES) {
            for(int i = 0; i < data.length; i++){
                if (data[i][0] != null) {
                    write("VENDA#" + data[i][0] + "#" + data[i][2]);
                    if (read().equalsIgnoreCase("fail")) {
                        System.out.println("ERRO NA VENDA PELA INTERFACE");
                    }
                }
            }
        }

        return;
    }


    public static void reserva(String id, String quant) {
      write("RESERVA#" + id + "#" + quant);
    }

    public static void devolucao(String id, String quant) {
      write("DEVOLUCAO#" + id + "#" + quant);
    }



}